<?php
require_once dirname(__FILE__).'/../../productPage/lib/ProductPageHelper.class.php';

class categoryPageActions extends myFrontendAction{
	
	public function executeTest(){
		$this->getRoute()->getObject();
	}

	public function preExecute(){
		parent::preExecute();
		$this->setLayout('layout_widgets_off');
	}

	static private $showByAr = array(1 => 1,6 => 6,9 => 9);
	static private $sortByAr = array('A-Z' => 'name', 'Price' => 'price');
									
	
	public function executeIndex( sfWebRequest $request ){

		$this->productPageHelper = new ProductPageHelper();

		$this->node = $this->getRoute()->getObject();

		$this->pager = new sfDoctrinePager( 'Product', $this->getShowBy() );

		$this->category = $this->getRoute()->getCategoryObject();
		$this->perpage = $this->getShowBy();
		$this->sorting = $this->getSort();

		$this->sortings = self::$sortByAr;
		$this->showby = self::$showByAr;

  		$this->pager->setQuery( $this->getCategoryProductsQuery() );
  		$this->pager->setPage($request->getParameter('page', 1));
  		$this->pager->init();

		$this->products = $this->pager->getResults();


		

  		if($this->pager->getNbResults() == 0){ // 3. redirect to first child if no PRODUCTS, but there are active SUB CATEGORIES
			  if(($menuElement = NavigationMenu::getInstance(NavigationMenu::SUB_MENU)
				->getElement(MenuElement::PRODUCT_GROUP, $this->category['id'])))
						foreach($menuElement->getSubElements() as $sub)
							if(!$sub->isHidden()){
								$this->redirect($sub->getLink());
							}
								
  		}



	}


	public function executeSortBy(sfWebRequest $request){
		if($request->hasParameter('sort') && in_array($request->getParameter('sort'), self::$sortByAr)){
			$this->setSort($request->getParameter('sort'));
		}

		$this->redirect(LinkGen::getInstance(LinkGen::PRODUCT_GROUP)->link($this->category['id']));
	}

	public function executeShowBy(sfWebRequest $request){

		if($request->hasParameter('show') && in_array($request->getParameter('show'), self::$showByAr)){
			$this->setShowby($request->getParameter('show'));
		}

		$this->redirect(LinkGen::getInstance(LinkGen::PRODUCT_GROUP)->link($this->category['id']));
	}

	protected function getShowBy(){
		if($this->getUser()->getAttribute('show')){
			return $this->getUser()->getAttribute('show');
		}else
			return 4;
	}

	protected function getShowIn(){
		if($this->getUser()->getAttribute('showin')){
			return $this->getUser()->getAttribute('showin');
		}else
			return 'list';
	}

	protected function getSort(){
		if( $this->getUser()->getAttribute('sort') ){
			return $this->getUser()->getAttribute('sort');
		}else{
			return current(self::$sortByAr);
		}
	}

	protected function setSort($sort){
		$this->getUser()->setAttribute('sort', $sort);
	}

	protected function setShowby($show){
		$this->getUser()->setAttribute('show', $show);
	}

	private function getCategoryProductsQuery(){

		$q = Doctrine::getTable('Product')
					->getAssociatedToCategoryProductsQuery( $this->getRoute()->getCategoryObject()->getId() );
		$q->andWhere($q->getRootAlias().'.is_active = ?', true);

		

		return $this->setCategoryQueryConstraints($q);
	}

	protected function setCategoryQueryConstraints( Doctrine_Query $q ){
		$q->innerJoin($q->getRootAlias().'.Translation t');

		Doctrine::getTable('Product')->addProductPicturesJoin( $q );
		$q->orderBy('p.pri');

//		switch($this->getSort()){
//			case 'name':
//				$q->orderBy('t.name asc');
//				break;
//			case 'price':
//				$q->orderBy($q->getRootAlias() . '.price asc');
//				break;
//		}


		return $q;
	}

	  
}